/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.ExamsEe;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.ExamsEeRepository;
import me.zhengjie.service.ExamsEeService;
import me.zhengjie.service.dto.ExamsEeDto;
import me.zhengjie.service.dto.ExamsEeQueryCriteria;
import me.zhengjie.service.mapstruct.ExamsEeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wuhj
* @date 2023-10-29
**/
@Service
@RequiredArgsConstructor
public class ExamsEeServiceImpl implements ExamsEeService {

    private final ExamsEeRepository examsEeRepository;
    private final ExamsEeMapper examsEeMapper;

    @Override
    public PageResult<ExamsEeDto> queryAll(ExamsEeQueryCriteria criteria, Pageable pageable){
        Page<ExamsEe> page = examsEeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(examsEeMapper::toDto));
    }

    @Override
    public List<ExamsEeDto> queryAll(ExamsEeQueryCriteria criteria){
        return examsEeMapper.toDto(examsEeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ExamsEeDto findById(Long id) {
        ExamsEe examsEe = examsEeRepository.findById(id).orElseGet(ExamsEe::new);
        ValidationUtil.isNull(examsEe.getId(),"ExamsEe","id",id);
        return examsEeMapper.toDto(examsEe);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ExamsEe resources) {
        examsEeRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ExamsEe resources) {
        ExamsEe examsEe = examsEeRepository.findById(resources.getId()).orElseGet(ExamsEe::new);
        ValidationUtil.isNull( examsEe.getId(),"ExamsEe","id",resources.getId());
        examsEe.copy(resources);
        examsEeRepository.save(examsEe);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            examsEeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ExamsEeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ExamsEeDto examsEe : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("证件ID", examsEe.getCardid());
            map.put("姓名", examsEe.getName());
            map.put("性别", examsEe.getGender());
            map.put("生日", examsEe.getBirthday());
            map.put("组", examsEe.getGroup());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}